--
-- Author: ping.wang
-- Date: 2019-01-10 18:41:10
--

local BranchSkillButton = MClass("BranchSkillButton")
local string_format = string.format
function BranchSkillButton:ctor()
    self.mTouchWidget = self:findChild("Panel/icon")
    self.mTouchWidget:setTouchEnabled(true)

    self.mSkillIcon = self:findChild("Panel/icon")
    self:setVisible(false)
    if self.mTimeline == nil then
        self.mTimeline = cc.CSLoader:createTimeline(Res.BattleBranchSkill)
        self:runAction(self.mTimeline)
        self.mTimeline:gotoFrameAndPause(0)
    end
end

--{skillCfg,rootSkillId}
function BranchSkillButton:setData(data)
    if not data then return end
    self.skillCfg = data.skillCfg
    self.rootSkillId = data.rootSkillId
    self.bBusy = false
    self:setVisible(true)
    local function clickCallBack(sender,eventType)
        if eventType == ccui.TouchEventType.began then
            self.bShowTips = false
            self.startTime = app.session:getServerTime() or app:getUTCTime()
            BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.BATTLE_CANCEL_AUTO)
            self.bBusy = true
        elseif eventType == ccui.TouchEventType.moved then

        elseif eventType == ccui.TouchEventType.ended or eventType == ccui.TouchEventType.canceled then
            -- --如果在取消按钮内，则取消
            self.duleTime = app.session:getServerTime() or app:getUTCTime()
            if eventType == ccui.TouchEventType.ended then
                if self.startTime then
                    local tempX, tempY = self:getPosition()
                    local posX, posY = self:getParent():toWorldPosition(tempX, tempY)
                    local skillId = self.skillCfg:getId()
                    if self.duleTime - self.startTime < Constant.BattleCancelInterval then
                        --通知上层按钮大小恢复原状 展开界面关闭 并且上层按钮的状态进入CD
                        --todo
                        BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.SKILL_SELECT, {skillId = self.rootSkillId, posX = posX, subSkillId = skillId,})
                        print("start do skill ",skillId,"and rootskill is ",self.rootSkillId)
                        BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.SKILL_HIDE_REPLACE_BTN,self.rootSkillId)
                    end
                end
            end
            self.bBusy = false
            self.startTime = nil
            self.bShowTips = false
            app:dispatchCustomEvent("EVENT_CLOSE_FLOAT")
        end
    end
    self.mTouchWidget:addTouchEventListener(clickCallBack)
    local icon = self.skillCfg:getIcon()
    if icon then
        self.mSkillIcon:loadTexture(string_format("%s%s.png",Res.skillIconPath,icon))
    end
end

function BranchSkillButton:playerAni()
    --开始播放上浮动画"in" 播放完毕后循环播放发光动画"breath"
    if self.mTimeline then
        self.mTimeline:play( "in", false )
        local info = self.mTimeline:getAnimationInfo("in")
        local closeDuration = info.endIndex - info.startIndex
        local action = cc.Sequence:create(cc.DelayTime:create(closeDuration/60),cc.CallFunc:create(
            function()
                --延迟时间到了之后需要播放本身自带的动画
                self.mTimeline:play( "breath", true )
            end)
        )

        self:runAction(action)
    end

end

return BranchSkillButton
